﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Collector.Analyzer
{
    class AnalyzerFileStream
    {

        public static byte[] ReadBytes(string filename)
        {
            if (!File.Exists(filename))
            {
                return EmptyArray<Byte>.Value;
            }                

            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            int filesize = (int)fs.Length;

            Log.Debug("Get filesize: " + filesize);
            byte[] data = br.ReadBytes(filesize);
            
            br.Close();
            fs.Close();

            return data;
        }

        // Useful in number of places that return an empty byte array to avoid unnecessary memory allocation.
        internal static class EmptyArray<T>
        {
            public static readonly T[] Value = new T[0];
        }
        /*
         *  {
         *     return EmptyArray<Byte>.Value;
         *  }
         */
    }
}
